//
//  OrderPendingTableViewCell.swift
//  OnceNectar
//
//  Created by 吴凌 on 2018/8/1.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class OrderPendingTableViewCell: UITableViewCell {

    @IBOutlet weak var scrollView: UIScrollView!
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    func showPending(model:myOrderModel,update:Bool) -> Void {
        if self.scrollView.subviews.count > 0 {
            if update {
                for item in self.scrollView.subviews {
                    item.removeFromSuperview()
                }
            }else {
                self.scrollView.reloadInputViews()
                return
            }
            
        }
        let status = Int(model.status!)
        var arr = Array<Dictionary<String,String>>()
        
        if status! >= 0  && status! < 4{
            var dic0 = Dictionary<String,String>()
            dic0["name"] = "提交订单"
            dic0["date"] = self.formatData(str: model.submitTime, index: 0)
            dic0["time"] = self.formatData(str: model.submitTime, index: 1)
            arr.append(dic0)
            
            if status! > 0 {
                var dic1 = Dictionary<String,String>()
                dic1["name"] = "付款成功"
                dic1["date"] = self.formatData(str: model.payTime, index: 0)
                dic1["time"] = self.formatData(str: model.payTime, index: 1)
                arr.append(dic1)
                
                var dic2 = Dictionary<String,String>()
                dic2["name"] = status == 1 ? "待发货" : "已发货"
                dic2["date"] = self.formatData(str: model.deliveryTime, index: 0)
                dic2["time"] = self.formatData(str: model.deliveryTime, index: 1)
                arr.append(dic2)
            }
            
            if status! > 1 {
                var dic3 = Dictionary<String,String>()
                dic3["name"] = status == 2 ? "待收货" : "已收货"
                dic3["date"] = self.formatData(str: model.receiveTime, index: 0)
                dic3["time"] = self.formatData(str: model.receiveTime, index: 1)
                arr.append(dic3)
            }
            
            if status! > 2 {
                var dic4 = Dictionary<String,String>()
                dic4["name"] = "已完成"
                dic4["date"] = ""
                dic4["time"] = ""
                arr.append(dic4)
            }
        }
        
        let fontSize:CGFloat = 10
        var itemWidth:CGFloat = 0
    
        let lineCount:CGFloat = CGFloat(arr.count - 1)
        let lineWidth:CGFloat = 20
        let btnLineSpase:CGFloat = 10
        let btnAllWidth = self.size.width - lineCount * lineWidth - CGFloat(3) * btnLineSpase * 2
        let width = btnAllWidth / CGFloat(3)
        itemWidth = CGFloat(width)
        
        let allWidth = itemWidth * CGFloat(arr.count) + lineWidth *  CGFloat(arr.count - 1) + CGFloat(arr.count) * btnLineSpase * 2
        self.scrollView.contentSize = CGSize.init(width: allWidth, height: self.mj_h)
        
        var count:Int = 0
        var lastX:CGFloat = 10
        var lineY:CGFloat = 0
        for dic in arr {
            let flag:Bool = count == arr.count - 1 ? true : false
            if count > 0 {
                let line:UIView = self.addLine(width: itemWidth, count: count, isLast: flag, lastX:lastX, lineY:lineY)
                self.scrollView.addSubview(line)
                lastX = line.mj_x + line.mj_w
            }
            
            let btn:UIButton = self.addBtn(width: itemWidth, count: count, isLast: flag, lastX: lastX)
            var btnStr:String = ""
            btnStr += dic["name"]!
            if dic["date"] != "" {
                btnStr += "\n" + dic["date"]!
            }
            
            if dic["time"] != "" {
                btnStr += "\n" + dic["time"]!
            }
            btn.titleLabel?.numberOfLines = 0
            btn.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
            btn.setTitle(btnStr, for: UIControlState.normal)
            self.scrollView.addSubview(btn)
            lastX = btn.mj_x + btn.mj_w
            lineY = btn.mj_y + btn.mj_h/2
            count += 1
        }
        if self.scrollView.contentOffset.x <= 0 {
            self.scrollView.setContentOffset(CGPoint.init(x: lastX, y: CGFloat(0)), animated: false)
        }
        
    }
    
    func addBtn(width:CGFloat,count:Int,isLast:Bool,lastX:CGFloat) -> UIButton {
        let btn:UIButton = UIButton.init(type: UIButtonType.custom)
        if isLast {
            btn.setBackgroundImage(UIImage.init(named: "orderProgress_finish_icon"), for: UIControlState.normal)
        } else {
            btn.setBackgroundImage(UIImage.init(named: "orderProgress_icon"), for: UIControlState.normal)
        }
        btn.frame = CGRect.init(x: lastX + 10, y: 0, width: width, height: width)
        btn.titleLabel?.textAlignment = NSTextAlignment.center
        btn.isEnabled = false
        return btn
    }
    
    func addLine(width:CGFloat,count:Int,isLast:Bool,lastX:CGFloat,lineY:CGFloat) -> UIView {
        let line:UIView = UIView.init()
        if !isLast {
            line.backgroundColor = ONColor.orederProgressLabelColor
        } else {
            line.backgroundColor = UIColor(hex: 0x00A0E9)
        }
        
        line.frame = CGRect.init(x: lastX + 10, y: lineY, width: 20, height: 1)
        
        return line
    }
    
    func formatData(str:String?,index:Int) -> String {
        if str != nil && str != "null" && str != "" {
            return str!.components(separatedBy: " ")[index]
        }
        return ""
    }
    
    func formatTime(str:String?,index:Int) -> String {
        if str != nil && str != "null" && str != "" {
            let string = NSString(string: str!)
            
            let timeSta:TimeInterval = string.doubleValue
            let dfmatter = DateFormatter()
            dfmatter.dateFormat="yyyy-MM-dd HH:mm:ss"
            let date:Date = Date.init(timeIntervalSince1970: timeSta)
            let datestr = dfmatter.string(from: date)
            return datestr.components(separatedBy: " ")[index]
        }
        return ""
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
